package cn.edu.dgut.css.springboottest2.Event;

import org.springframework.context.annotation.Bean;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * @Author:GooGa
 * @Data:2020/11/1 22:43
 */
public class EventConfig {
    @Bean
    public Executor executor(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); //此方法返回可用处理器的虚拟机的最大数量; 不小于1
        int core = Runtime.getRuntime().availableProcessors();
        executor.setCorePoolSize(core);     //设置核心线程数
        executor.setMaxPoolSize(core*2 + 1);//设置最大线程数
        executor.setKeepAliveSeconds(3);    //除核心线程外的线程存活时间
        executor.setQueueCapacity(40);      //如果传入值大于0，底层队列使用的是LinkedBlockingQueue,否则默认使用SynchronousQueue
        executor.setThreadNamePrefix("thread-"); //线程名称前缀
//        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());    //设置拒绝策略
        return executor;
    }


    @Bean(AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME)
    public ApplicationEventMulticaster customApplicationEventMulticaster(Executor executor) {
        SimpleApplicationEventMulticaster eventMulticaster = new SimpleApplicationEventMulticaster();
        eventMulticaster.setTaskExecutor(executor);
        return eventMulticaster;
    }

    @Bean
    public NoticeListener createNoticeListener() {
        return new NoticeListener();
    }

    @Bean
    public ApplicationReadyEventListener createApplicationReadyEventListener() {
        return new ApplicationReadyEventListener();
    }


}


